package com.woniuxy.goods.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.goods.entity.Goods;
import com.woniuxy.goods.mapper.GoodsMapper;
import com.woniuxy.goods.service.GoodsService;
import com.woniuxy.goods.service.PointsService;
import com.woniuxy.goods.vo.BuyGoodsVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private PointsService pointsService;

    @Override
    public PageInfo<Goods> findAll(Integer currentPage, Integer pageSize, String name) {
        PageHelper.startPage(currentPage, pageSize);
        List<Goods> list = goodsMapper.findAll(name);
        PageInfo<Goods> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public boolean add(Goods goods) {
        return goodsMapper.add(goods);
    }

    @Override
    public boolean edit(Goods goods) {
        return goodsMapper.edit(goods);
    }

    @Override
    public boolean delete(Integer id) {
        return goodsMapper.delete(id);
    }

    @Override
    @Transactional
    public boolean buy(BuyGoodsVo buyGoodsVo) {
        goodsMapper.buy(buyGoodsVo);
        goodsMapper.editGoodsNum(buyGoodsVo.getId(), buyGoodsVo.getNum());
        pointsService.edit(buyGoodsVo);
        return true;
    }

    @Override
    public PageInfo<BuyGoodsVo> all(Integer currentPage, Integer pageSize, String name) {
        PageHelper.startPage(currentPage, pageSize);
        List<BuyGoodsVo> list = goodsMapper.all(name);
        PageInfo<BuyGoodsVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public boolean editState(Integer id) {
        return goodsMapper.editState(id);
    }


}
